package com.example.im.dao;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.example.im.entity.ChatRecordEntire;

import java.util.List;

@Dao
public interface ChatRecordEntireDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    Long insert(ChatRecordEntire recordEntire);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(List<ChatRecordEntire> recordEntires);

    @Update(onConflict = OnConflictStrategy.REPLACE)
    void update(ChatRecordEntire recordEntire);

    @Delete
    void delete(ChatRecordEntire recordEntire);

    @Query("DELETE FROM ChatRecordEntire WHERE last_id= :lastId")
    void delete(int lastId);

    @Query("SELECT * FROM ChatRecordEntire WHERE id= :id")
    ChatRecordEntire getChatRecordEntireByID(int id);

    @Query("SELECT * FROM ChatRecordEntire WHERE last_id= (SELECT id FROM ChatRecordLast WHERE f_id = :f_id)")
    List<ChatRecordEntire> selectChatRecordEntire(int f_id);


//    @Query("SELECT * FROM ChatRecordEntire WHERE last_id= (SELECT id FROM ChatRecordLast WHERE f_id = (SELECT id FROM FriendOrRoom WHERE account=:account AND type=:type) ) ORDER BY id DESC LIMIT 1")
//    ChatRecordEntire selectChatRecordEntireLastTime(String account, int type);

    @Query("SELECT * FROM ChatRecordEntire WHERE last_id= :lastId ORDER BY id DESC LIMIT 1")
    ChatRecordEntire selectChatRecordEntireLastTime(int lastId);

    @Query("UPDATE ChatRecordEntire SET status= :newStatus WHERE status = :oldStatus AND last_id = (SELECT id FROM ChatRecordLast WHERE f_id = :f_id) ")
    void upDateChatRecordEntireStatus(int newStatus, int oldStatus, int f_id);

    @Query("UPDATE ChatRecordEntire SET body= :body WHERE id = :id")
    void upDateChatRecordEntireBody(String body, int id);

    @Query("SELECT COUNT(*) FROM ChatRecordEntire WHERE status= 1 AND last_id=:lastId")
    int count(int lastId);

    @Query("SELECT COUNT(*) FROM ChatRecordEntire WHERE status= 1")
    int count();


    @Query("SELECT * FROM ChatRecordEntire WHERE last_id= :lastId and status = 1 and itel is not null")
    List<ChatRecordEntire> selectChatRecordEntireItel(int lastId);

}
